/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.msg;

import org.rails.extjs.Component;
import org.rails.extjs.Option;
import org.rails.extjs.javascript.Function;
import org.rails.extjs.javascript.JavaScript;
import org.rails.extjs.javascript.Switch;

public class MessageBox extends Component {

	public static String ICON_QUESTION = "Ext.Msg.QUESTION";
	public static String ICON_INFO = "Ext.Msg.INFO";
	public static String ICON_ERROR = "Ext.Msg.ERROR";
	public static String ICON_WARNING = "Ext.Msg.WARNING";

	public static String BUTTON_CANCEL = "Ext.Msg.CANCEL";
	public static String BUTTON_OK = "Ext.Msg.OK";
	public static String BUTTON_OK_CANCEL = "Ext.Msg.OKCANCEL";
	public static String BUTTON_YES_NO = "Ext.Msg.YESNO";
	public static String BUTTON_YES_NO_CANCEL = "Ext.Msg.YESNOCANCEL";

	private Function function;
	private final Switch sw = new Switch("btn");

	public MessageBox(String buttons, Object title, Object msg) {
		super();
		setMsg(msg);
		setTitle(title);
		setButtons(buttons);
	}

	public MessageBox(String icon, String buttons, Object title, Object msg) {
		super();		
		setIcon(icon);
		setMsg(msg);
		setTitle(title);
		setButtons(buttons);
	}
	
	public void setTitle(Object title){
		addOption("title",title);
	}
	
	public Object getTitle(){
		return getOption("title");
	}
	
	public void setFn(Function fn) {
		addOption("fn", fn);
	}

	public Function getFn() {
		return (Function) getOption("fn");
	}

	public void setMsg(Object msg) {
		addOption("msg", msg);
	}

	public Object getMsg() {
		return getOption("msg");
	}

	public void setButtonText(Object buttonText) {
		addOption("buttonText", buttonText);
	}

	public Object getButtonText() {
		return getOption("buttonText");
	}

	public void setDefaultTextHeight(Integer defaultTextHeight) {
		addOption("defaultTextHeight", defaultTextHeight);
	}

	public Integer getDefaultTextHeight() {
		return getInteger("defaultTextHeight", 75);
	}

	public void setMinProgressWidth(Integer minProgressWidth) {
		addOption("minProgressWidth", minProgressWidth);
	}

	public Integer getMinProgressWidth() {
		return getInteger("minProgressWidth", 250);
	}

	public void setMinPromptWidth(Integer minPromptWidth) {
		addOption("minPromptWidth", minPromptWidth);
	}

	public Integer getMinPromptWidth() {
		return getInteger("minPromptWidth", 250);
	}

	public static JavaScript alert(Object title, Object msg, JavaScript js,
			JavaScript scope) {
		final StringBuffer sbf = new StringBuffer("Ext.Msg.alert(");
		sbf.append(escape(title));
		sbf.append(",");
		sbf.append(escape(msg));
		sbf.append(",");
		if (js != null)
			sbf.append(new Function(js).getScript());
		else
			sbf.append(new Function().getScript());

		if (scope != null)
			sbf.append("," + scope.getScript());

		sbf.append(");");
		return new JavaScript(sbf.toString());
	}

	public static JavaScript prompt(Object title, Object msg, JavaScript ok,
			JavaScript cancel, JavaScript scope, Integer multiline, Object value) {
		final Switch swh = new Switch("btn");
		if (ok != null)
			swh.caseValue("'ok'", ok);
		if (cancel != null)
			swh.caseValue("'cancel'", cancel);

		final StringBuffer sbf = new StringBuffer("Ext.Msg.prompt(");
		sbf.append(escape(title));
		sbf.append(",");
		sbf.append(escape(msg));
		sbf.append(",");
		sbf.append(new Function(swh, "btn,text").getScript());
		sbf.append(",");
		if (scope != null)
			sbf.append(scope.getScript());
		else
			sbf.append("this");

		sbf.append(",");
		if (multiline != null)
			sbf.append(multiline);
		else
			sbf.append(false);

		sbf.append(",");
		sbf.append(escape(value));
		sbf.append(");");
		return new JavaScript(sbf.toString());
	}

	public static JavaScript confirm(Object title, Object msg, JavaScript yes,
			JavaScript no, JavaScript scope) {
		final Switch swh = new Switch("btn");
		if (yes != null)
			swh.caseValue("'yes'", yes);
		if (no != null)
			swh.caseValue("'no'", no);

		final StringBuffer sbf = new StringBuffer("Ext.Msg.confirm(");
		sbf.append(escape(title));
		sbf.append(",");
		sbf.append(escape(msg));
		sbf.append(",");
		sbf.append(new Function(swh, "btn,text").getScript());
		if (scope != null)
			sbf.append("," + scope.getScript());

		sbf.append(");");
		return new JavaScript(sbf.toString());
	}

	public static JavaScript wait(Object title, Object msg, Option option) {
		final StringBuffer sbf = new StringBuffer("Ext.Msg.wait(");
		sbf.append(escape(msg));
		sbf.append(",");
		sbf.append(escape(title));

		if (option != null)
			sbf.append("," + option.createJson());

		sbf.append(");");
		return new JavaScript(sbf.toString());
	}

	public void registYes(JavaScript javaScript) {
		sw.caseValue("'yes'", javaScript);
	}

	public void registNo(JavaScript javaScript) {
		sw.caseValue("'no'", javaScript);
	}

	public void registCancel(JavaScript javaScript) {
		sw.caseValue("'cancel'", javaScript);
	}

	public void registOK(JavaScript javaScript) {
		sw.caseValue("'ok'", javaScript);
	}

	public void registOther(JavaScript javaScript) {
		sw.otherValue(javaScript);
	}

	public void setPrompt(Boolean isPrompt) {
		addOption("prompt", isPrompt);
	}

	public Boolean isPrompt() {
		return getBoolean("prompt", false);
	}

	public void setMultiline(Integer line) {
		addOption("multiline", line);
	}

	public Integer getMultiline() {
		return getInteger("multiline", 0);
	}

	public String getMessage() {
		return getString("msg", "");
	}

	public void setMessage(String message) {
		addOption("msg", message);
	}

	public String getButtons() {
		return getString("buttons", "");
	}

	public void setButtons(String buttons) {
		addOption("buttons", new JavaScript(buttons));
	}

	public String getIcon() {
		return getString("icon", "");
	}

	public void setIcon(String icon) {
		addOption("icon", new JavaScript(icon));
	}

	public Boolean isModal() {
		return getBoolean("modal", true);
	}

	public void setModal(Boolean modal) {
		addOption("modal", modal);
	}

	public void setClosable(Boolean closable) {
		addOption("closable", closable);
	}

	public Boolean isClosable() {
		return getBoolean("closable", false);
	}

	@Override
	public String getScript() {
		createItems();
		script = new StringBuffer("");
		function = new Function(sw, "btn,text,opt");
		addOption("fn", function);
		script.append("Ext.Msg.show(");
		script.append(createOptions());
		script.append(");");
		return script.toString();
	}

}
